#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define PATH_FIFO "/home/yujiu/fifo"
void if_error(int,char*,int);
int main(){
	int res=0;
	res=access(PATH_FIFO,F_OK);
	if_error(res,"access",-1);	
	int fd=open(PATH_FIFO,O_WRONLY);
	if_error(fd,"read",fd);
	char buf[128];
	ssize_t byte=0;
	while(1){
		memset(buf,0,sizeof(buf));
		printf("input:\n");
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1]='\0';
		byte=write(fd,buf,strlen(buf));
		if_error(byte,"write",fd);
		if(strcmp(buf,"exit")==0){
			break;
		}
	}
	close(fd);
	return 0;
}
void if_error(int res,char* str,int fd){
	if(res==-1){           
        perror(str);
		if(fd!=-1){
			close(fd);
		}
        exit(1);           
	}   
}
